<template>
	<view class="login">
		<!-- #ifndef H5 -->
		<view style="width:100%;height: var(--status-bar-height);"></view>
		<!-- #endif -->
		<image :src="$util.img($store.state.loginImg)" mode="widthFix"></image>
		<view class="item-input">
			<input type="number" v-model="mobile" placeholder-class="place-holderClass" placeholder="请输入手机号"/>
		</view>
		<view class="item-input">
			<input type="text" v-model="code" placeholder-class="place-holderClass" placeholder="请输入验证码"/>
			<view class="item-time" @click="verification">
				{{ time?time + 's已发送':'获取验证码'}}
			</view>
		</view>
		<view class="item-input">
			<input type="text" v-model="password" password placeholder-class="place-holderClass" placeholder="请输入密码"/>
		</view>
		<view class="item-input">
			<input type="text" v-model="invite_code" placeholder-class="place-holderClass" placeholder="请输入邀请码(选填)" :disabled="disabled"/>
		</view>
		<button class="login" type="default" @click="registerDown">注册</button>
		<view class="register">
			<text>已有账号？</text>
			<text style="color:#FF6F30;" @click="$util.redirectTo('/pages/login/login')">立即登录</text>
		</view>
	</view>
</template>

<script>
	export default{
		data(){
			return {
				mobile:'',//手机号
				code:'',//验证码
				invite_code:'',//邀请码
				password:'',//密码
				time:'',//验证码返回时间
				repeatJudge:false,//防重复
				// 手机号做正则判断
				pattern:/^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/,
				disabled:false,
				res:{}
			}
		},
		onLoad(option){
			if(option.invite_code){
				this.invite_code = option.invite_code
				this.disabled = true;
			}
		},
		methods:{
			//获取验证码
			verification(){
				if(!this.pattern.test(this.mobile)) {
					uni.showToast({title:'请输入正确的手机号',icon:'none'})
					return false ;
				}
				this.$http.sendrequest('/login/sms_verification_code',{
					mobile:this.mobile,
					type:'register',
				}).then(res=>{
					this.res = JSON.stringify(res);
					uni.showToast({title:res.msg,icon:'none'})
					if(res.code == 1){
						this.time = 60;
						let die = setInterval(()=>{
							this.time -= 1;
							if(this.time == 0) clearInterval(die) 
						},1000)
					}
				})
			},
			//注册
			registerDown(){
				let title = '';
				if(!this.mobile || !this.code || !this.password){
					title = '输入不能为空'
				} 
				else{
					if(!this.pattern.test(this.mobile)) title = '请输入正确的手机号'; 
				} 
				if(title) {
					uni.showToast({title:title,icon:'none'})
					return false
				}
				if(this.repeatJudge) return false;
				this.repeatJudge = true
				this.$http.sendrequest('/login/phone_register',{
					mobile:this.mobile,
					code:this.code,
					invite_code:this.invite_code,
					password:this.password
				}).then(res=>{
					uni.showToast({title:res.msg,icon:'none'})
					this.repeatJudge = false;
					if(res.code == 1){
						uni.setStorageSync('token',res.data)
						setTimeout(()=>{
							this.$util.redirectTo('/pages/login/login',{},'redirectTo');
						},500)
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	@import './css/diy-login.scss'
</style>
